<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Show extends Controller
{

    public function index($id)
    {
        $uid = Session::get('id');
        if ($uid == NULL) {
            return view('login/index');
        }

//        halt($uid);
        $a = Db::field('u.id uid,m.id id,u.username username,photo,content,time,collectnum,commentnum,transpondnum,agreenum,readnum,hot,src_id')
            ->table(['wb_message' => 'm', 'wb_buser' => 'u', 'wb_buserinfo' => 'i'])
            ->where('m.uid = u.id and m.id = ' . $id . ' and i.uid=u.id ')
            ->find();

        $path = Db::field('id pid,path')
            ->table(['wb_mpicture'])
            ->where('mid= ' . $a['id'])
            ->find();
//            halt($path);

        $res = Db::field('co.id cid,co.uid uid,co.mid mid,co.time time,content,u.username username,i.photo photo,co.cid rid')
            ->table(['wb_comments' => 'co', 'wb_buser' => 'u', 'wb_buserinfo' => 'i'])
            ->where('co.mid = ' . $id . ' and co.uid = u.id and u.id = i.uid')
            ->select();
//        halt($res);
        foreach ($res as $k => $v) {
            $data = Db::field('u.username username,u.id uid')
                ->table(['wb_comments' => 'c', 'wb_buser' => 'u'])
                ->where('c.id','=',$v['rid'])
                ->where('u.id = c.uid')
                ->find();
            $res[$k]['oldname'] = $data;
        }
//        halt($res);
        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $a['uid'])
            ->find();
//        halt($arr);
        if (!empty($arr)){
            $role = implode("", $arr);
        }

        $uid = Session::get('id');
        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = ' . $uid)
            ->find();
        $photo = implode("", $p);
        $username = Session::get('username');

        return view('/show/index', [
            'data' => $a,
            'path' => $path,
            'res' => $res,
            'id' => $uid,
            'username'=>$username,
            'photo'=>$photo,
            'role' => $role

        ]);
    }

    //回复
    public function replay()
    {
        if (!request()->isAjax()) {
            $this->error('页面不存在');

        }

        $uid = Session::get('id');

        if (empty($uid)) {
            return $data = false;
        }
        $data = \request()->post();
        $data['time'] = date('Y-m-d H:i:s');
        $data['uid'] = $uid;
//        halt($data);
        $id = db()->table('wb_comments')->insertGetId($data);

        $res = Db::field('photo')
            ->table(['wb_buserinfo' => 'i'])
            ->where('i.uid = ' . $uid)
            ->find();
        $data = array(
            'cid' => $id,
            'username' => Session::get('username'),
            'content' => $data['content'],
            'uid' => $uid,
            'photo' => $res['photo'],
            'time' => date('Y-m-d H:i:s'),
        );
        return $data;


    }


}
